﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_GongSiBiaoService : It_GongSiBiaoService
    {
        public readonly MyContext _Context;
        public t_GongSiBiaoService(MyContext myContext)
        {
            _Context = myContext;
        }
        public t_GongSiBiao Add(t_GongSiBiao t_GongSiBiao)
        {
            _Context.t_GongSiBiao.Add(t_GongSiBiao);
            var test = _Context.SaveChanges(); ;
            return _Context.t_GongSiBiao.FirstOrDefault(x => x.BeiZhu1 == t_GongSiBiao.BeiZhu1 && x.BeiZhu2 == t_GongSiBiao.BeiZhu2 && x.BeiZhu3 == t_GongSiBiao.BeiZhu3 && x.BeiZhu4 == t_GongSiBiao.BeiZhu4 && x.BeiZhu5 == t_GongSiBiao.BeiZhu5 && x.BeiZhu6 == t_GongSiBiao.BeiZhu6 && x.BeiZhu7 == t_GongSiBiao.BeiZhu7 && x.BeiZhu8 == t_GongSiBiao.BeiZhu8 && x.BeiZhu9 == t_GongSiBiao.BeiZhu9 && x.BeiZhu10 == t_GongSiBiao.BeiZhu10 && x.GongSiMingCheng == t_GongSiBiao.GongSiMingCheng && x.GongSiJianJie == t_GongSiBiao.GongSiJianJie && x.GongSiDiZhi == t_GongSiBiao.GongSiDiZhi && x.LianXiFangShi == t_GongSiBiao.LianXiFangShi && x.FaRen == t_GongSiBiao.FaRen && x.ChengLiShiJian == t_GongSiBiao.ChengLiShiJian && x.GuanFangWangZhan == t_GongSiBiao.GuanFangWangZhan && x.GongSiGuiMo == t_GongSiBiao.GongSiGuiMo);
        }

        public t_GongSiBiao GetById(int Id)
        {
            return _Context.t_GongSiBiao.FirstOrDefault(x => x.Id == Id);
        }

        public List<t_GongSiBiao> GetList()
        {
            return _Context.t_GongSiBiao.Include(x => x.t_BuMenBiaos).Include(x=>x.t_BuMenBiaos).Where(x => x.Id != 1).ToList();
        }
        public string GetListWithFenYe(t_GongSiBiao t_GongSiBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_GongSiBiao);
            var list = new List<t_GongSiBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_GongSiBiao.Where(returnwhere).ToList();
            }
            //switch (column)
            //{
            //    case 0:
            //        if (dir == "asc")
            //        {
            //            list = list.OrderBy(x => x.GongSiMingCheng).ToList();
            //        }
            //        else
            //        {
            //            list = list.OrderByDescending(x => x.GongSiMingCheng).ToList();
            //        }
            //        break;
            //    case 1:
            //        if (dir == "asc")
            //        {
            //            list = list.OrderBy(x => x.ChengLiShiJian).ToList();
            //        }
            //        else
            //        {
            //            list = list.OrderByDescending(x => x.ChengLiShiJian).ToList();
            //        }
            //        break;
            //    case 2:
            //        if (dir == "asc")
            //        {
            //            list = list.OrderBy(x => x.LianXiFangShi).ToList();
            //        }
            //        else
            //        {
            //            list = list.OrderByDescending(x => x.LianXiFangShi).ToList();
            //        }
            //        break;
            //    case 3:
            //        if (dir == "asc")
            //        {
            //            list = list.OrderBy(x => x.FaRen).ToList();
            //        }
            //        else
            //        {
            //            list = list.OrderByDescending(x => x.FaRen).ToList();
            //        }
            //        break;
            //    case 4:
            //        if (dir == "asc")
            //        {
            //            list = list.OrderBy(x => x.GongSiGuiMo).ToList();
            //        }
            //        else
            //        {
            //            list = list.OrderByDescending(x => x.GongSiGuiMo).ToList();
            //        }
            //        break;
            //    case 5:
            //        if (dir == "asc")
            //        {
            //            list = list.OrderBy(x => x.GuanFangWangZhan).ToList();
            //        }
            //        else
            //        {
            //            list = list.OrderByDescending(x => x.GuanFangWangZhan).ToList();
            //        }
            //        break;
            //    default:
            //        break;
            //}
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }

        public string ReturnWhere(t_GongSiBiao t_GongSiBiao)
        {
            string returnstr = " Id!=1";
            if (t_GongSiBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_GongSiBiao.BeiZhu1 + "\")";
            }
            if (t_GongSiBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_GongSiBiao.BeiZhu2 + "\")";
            }
            if (t_GongSiBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_GongSiBiao.BeiZhu3 + "\")";
            }
            if (t_GongSiBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_GongSiBiao.BeiZhu4 + "\")";
            }
            if (t_GongSiBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_GongSiBiao.BeiZhu5 + "\")";
            }
            if (t_GongSiBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(\"" + t_GongSiBiao.BeiZhu6 + "\")";
            }
            if (t_GongSiBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(\"" + t_GongSiBiao.BeiZhu7 + "\")";
            }
            if (t_GongSiBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(\"" + t_GongSiBiao.BeiZhu8 + "\")";
            }
            if (t_GongSiBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(\"" + t_GongSiBiao.BeiZhu9 + "\")";
            }
            if (t_GongSiBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(\"" + t_GongSiBiao.BeiZhu10 + "\")";
            }
            if (t_GongSiBiao.Id != 0)
            {
                returnstr = returnstr + " and Id == " + t_GongSiBiao.Id + "";
            }
            if (t_GongSiBiao.GongSiMingCheng != null)
            {
                returnstr = returnstr + " and GongSiMingCheng.Contains(\"" + t_GongSiBiao.GongSiMingCheng + "\")";
            }
            if (t_GongSiBiao.GongSiJianJie != null)
            {
                returnstr = returnstr + " and GongSiJianJie.Contains(\"" + t_GongSiBiao.GongSiJianJie + "\")";
            }
            if (t_GongSiBiao.GongSiDiZhi != null)
            {
                returnstr = returnstr + " and GongSiDiZhi.Contains(\"" + t_GongSiBiao.GongSiDiZhi + "\")";
            }
            if (t_GongSiBiao.LianXiFangShi != null)
            {
                returnstr = returnstr + " and LianXiFangShi.Contains(\"" + t_GongSiBiao.LianXiFangShi + "\")";
            }
            if (t_GongSiBiao.FaRen != null)
            {
                returnstr = returnstr + " and FaRen.Contains(\"" + t_GongSiBiao.FaRen + "\")";
            }
            if (t_GongSiBiao.ChengLiShiJian != null)
            {
                returnstr = returnstr + " and ChengLiShiJian.Contains(\"" + t_GongSiBiao.ChengLiShiJian + "\")";
            }
            if (t_GongSiBiao.GuanFangWangZhan != null)
            {
                returnstr = returnstr + " and GuanFangWangZhan.Contains(\"" + t_GongSiBiao.GuanFangWangZhan + "\")";
            }
            if (t_GongSiBiao.GongSiGuiMo != null)
            {
                returnstr = returnstr + " and GongSiGuiMo.Contains(\"" + t_GongSiBiao.GongSiGuiMo + "\")";
            }
            return returnstr;
        }


    }
}
